/*-
 * Copyright (c) 2006, 2007 FTS Team
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 4. Neither the name of the FTS Team nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *      RetrieverTest.cpp
 *
 * $FTS$
 */

// C Includes
#include <fcntl.h>
#include <math.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>

// Local Includes
#include <FSDumper.hpp>
#include <FileDocumentStorage.hpp>
#include <IRConfig.hpp>
#include <MemoryDocumentInfoStorage.hpp>
#include <MemoryInvIndexStorage.hpp>
#include <Retriever.hpp>
#include <OutputDataCollector.hpp>
#include <QueryParser.hpp>
#include <ParseException.hpp>
#include <SnippetCollector.hpp>
#include <StopWords.hpp>
#include <TimeProfiler.hpp>

using namespace FTSS; // FullTextSearchSystem

class XMLOutputDataCollector:
  public OutputDataCollector
{
private:
	FILE * fOutputStream;
	INT_32 iDocNum;
	INT_64 iSec;
	INT_64 iUSec;
public:

	// Constructor
	XMLOutputDataCollector(FILE * fIOutputStream): fOutputStream(fIOutputStream), iDocNum(0) { ;; }

	// Destructor
	~XMLOutputDataCollector() throw() { ;; }
private:
	/**
	  @brief Start search results collection
	  @param iDocumentsCount - Number of documents
	  @param aTerms - List of terms
	  @param iTermsCount - Munber of terms in list
	*/
	void StartOutput(const INT_32 & iDocumentsCount, const TermStatistic * aTerms, const INT_32 & iTermsCount)
	{
		iDocNum = 0;
		fprintf(fOutputStream, "<?xml version=\"1.0\" encoding=\"windows-1251\"?>\n<?xml-stylesheet title=\"XSL_formatting\" type=\"text/xsl\" href=\"FTSS.xsl\"?>\n<FTSSP version=\"1.0\">\n");
		fprintf(fOutputStream, "\t<TM>%lu.%06lu</TM>\n", long(iSec), long(iUSec));
		fprintf(fOutputStream, "\t<TL N=\"%d\">\n", iTermsCount);
		for (INT_32 iI = 0; iI < iTermsCount; ++iI)
		{
			fprintf(fOutputStream, "\t\t<T N=\"%d\" W=\"", iI);
			fwrite(aTerms[iI].term, aTerms[iI].term_length, 1, fOutputStream);
			fprintf(fOutputStream, "\" DN=\"%d\" DG=\"%d\"/>\n", aTerms[iI].doc_count, aTerms[iI].doc_group_count);
		}
		fprintf(fOutputStream, "\t</TL>\n");
		fprintf(fOutputStream, "\t<RL N=\"%d\">\n", iDocumentsCount);
	}

	/**
	  @brief Start new document and store document properties
	  @param pDocumentInfo - Information about document
	  @param dRank - document relevance
	*/
	void StartDocument(const DocumentInfo * pDocumentInfo, const W_FLOAT & dRank)
	{
		CCHAR_P szEncoding = "cp1251";
		CCHAR_P szContentType = "text/plain";
		CCHAR_P szLanguage = "ru";
		fprintf(fOutputStream, "\t\t<D N=\"%d\" RV=\"%f\" ID=\"%d\" S=\"%d\" E=\"%s\" L=\"%s\" CT=\"%s\" TMC=\"%d\" TMM=\"%d\" TMA=\"%d\" CI=\"%d\" SI=\"%d\">\n\t\t\t<SS>\n",
		                       ++iDocNum,
		                       dRank,
		                       INT_32(pDocumentInfo -> doc_id),
		                       pDocumentInfo -> size,
		                       szEncoding,
		                       szContentType,
		                       szLanguage,
		                       UINT_32(pDocumentInfo -> ctime),
		                       UINT_32(pDocumentInfo -> mtime),
		                       UINT_32(pDocumentInfo -> atime),
		                       pDocumentInfo -> ci,
		                       UINT_32(pDocumentInfo -> doc_core_id)
		                       );
	}

	/**
	  @brief End of document
	*/
	void EndDocument() { fprintf(fOutputStream, "\t\t\t</SS>\n\t\t</D>\n"); }

	/**
	  @brief Start of snippet
	*/
	void StartSnippet() { fprintf(fOutputStream, "\t\t\t\t<S>"); }

	/**
	  @brief Add snippet data to document
	  @param sSnippet - snippet data
	  @param iLength - snippet data length
	*/
	void AddSnippetData(CCHAR_P sSnippet, const Size_t & iLength) { fwrite(sSnippet, 1, iLength, fOutputStream); }

	/**
	  @brief Add term to document
	  @param sSnippet - snippet
	  @param iLength - Term length
	*/
	void AddTerm(CCHAR_P sSnippet, const Size_t & iLength)
	{
		fprintf(fOutputStream, "<B>");
		fwrite(sSnippet, 1, iLength, fOutputStream);
		fprintf(fOutputStream, "</B>");
	}

	/** End of snippet */
	void EndSnippet() { fprintf(fOutputStream, "</S>\n"); }

	/**
	  @brief End of search result collection
	*/
	void EndOutput()
	{
		fprintf(fOutputStream, "\t</RL>\n");
		fprintf(fOutputStream, "</FTSSP>\n");
	}

	/**
	  @brief Store query execution time
	  @param iISec - number of seconds
	  @param iIUSec - number of microseconds
	*/
	void ExecTime(const UINT_64 & iISec, const UINT_64 & iIUSec)
	{
		iSec = iISec;
		iUSec = iIUSec;
	}

	/**
	  @brief Set error code
	  @param iErrorCode - unix error code
	*/
	void SetErrorCode(const INT_32 & iErrorCode)
	{
		;;
	}
};

int main(int argc, char ** argv)
{
	if (argc != 6 && argc != 7) { fprintf(stdout, "usage: %s InvIndex.ftss DocumentsDB.ftss DocStorIndex.ftss DocStorData.ftss StopWords.ftss\n       %s InvIndex.ftss DocumentsDB.ftss DocStorIndex.ftss DocStorData.ftss StopWords.ftss 'query'\n", argv[0], argv[0]); return EX_USAGE; }

	try
	{
		fprintf(stdout, "Initializing.\n");
#ifdef _NO_LEMMATIZER
		fprintf(stdout, "Without lemmatizer.\n");
		FTSSLemmatizer     oLemmatizer;
#else
		fprintf(stdout, "Lemmatizer...");  fflush(stdout);
		FTSSLemmatizer     oLemmatizer("/usr/local/share/lemmatizer", C_RUS_GRAM_TAB | C_ENG_GRAM_TAB);
		fprintf(stdout, "OK\n");
#endif
		fprintf(stdout, "Inverted index...");  fflush(stdout);
		// Load main index in memory
		MemoryInvIndexStorage oStorage;
		int iFileHandle = open(argv[1], O_RDONLY, 0644);
		if (iFileHandle != -1)
		{
			FSDumper oDumper(iFileHandle);
			if (oStorage.Restore(&oDumper) == -1)
			{
				fprintf(stderr, "Cannot restore index `%s'.\n", argv[1]);
				return EX_SOFTWARE;
			}
			close(iFileHandle);
		}
		else { fprintf(stderr, "Cannot open index file.\n"); return EX_SOFTWARE; }
		fprintf(stdout, "OK\n");

		fprintf(stdout, "Document properties storage..."); fflush(stdout);
		// Load document properties in memory
		MemoryDocumentInfoStorage oDocInfoStorage;
		iFileHandle = open(argv[2], O_RDONLY, 0644);
		if (iFileHandle != -1)
		{
			FSDumper oDumper(iFileHandle);
			if (oDocInfoStorage.Restore(&oDumper) == -1)
			{
				fprintf(stderr, "Cannot restore index `%s'.\n", argv[2]);
				return EX_SOFTWARE;
			}
			close(iFileHandle);
		}
		else { fprintf(stderr, "Cannot open index file.\n"); return EX_SOFTWARE; }
		fprintf(stdout, "OK\n");

		fprintf(stdout, "Document storage...");  fflush(stdout);
		FileDocumentStorage oDocumentStorage(argv[3], argv[4], C_READ_MODE);
		fprintf(stdout, "OK\n");

		fprintf(stdout, "Stop words storage...");  fflush(stdout);
		// Load stop-words list in memory
		StopWords oStopWords(oLemmatizer);
		iFileHandle = open(argv[5], O_RDONLY, 0644);
		if (iFileHandle != -1)
		{
			FSDumper oDumper(iFileHandle);
			if (oStopWords.Restore(&oDumper) == -1)
			{
				fprintf(stderr, "Cannot restore stopwords `%s'.\n", argv[3]);
				return EX_SOFTWARE;
			}
		}
		else { fprintf(stderr, "Cannot restore stopwords.\n"); return EX_SOFTWARE; }
		fprintf(stdout, "OK\n");

		IRConfig     oConfig(10);
		TimeProfiler oTimeProfiler(10);

		fprintf(stdout, "Retriever..."); fflush(stdout);
		Retriever oRetriever(oConfig, oLemmatizer, &oStorage, &oDocInfoStorage, &oDocumentStorage, oStopWords, oTimeProfiler);
		fprintf(stdout, "OK\nReady to work.\n");

		CHAR_8 szBuf[1024 + 1];
		setvbuf(stdout, NULL, _IONBF, 0);
		if (argc == 7)
		{
			XMLOutputDataCollector oXMLCollector(stdout);
			SnippetCollector oSnippetCollector(&oXMLCollector, &oDocumentStorage);
			oRetriever.Search(argv[6], strlen(argv[6]), oSnippetCollector);
			fprintf(stdout, "\n\n");
		}
		else
		{
			for(;;)
			{
				fprintf(stdout, "> ");
				XMLOutputDataCollector oXMLCollector(stdout);
				SnippetCollector oSnippetCollector(&oXMLCollector, &oDocumentStorage);
				if (fgets(szBuf, 1024, stdin) != NULL)
				{
					INT_32 iQueryLength = strlen(szBuf) - 1;
					szBuf[iQueryLength] = '\0';
					oRetriever.Search(szBuf, iQueryLength, oSnippetCollector);
					fprintf(stdout, "\n\n");
				}
			}
		}
	}
	catch(ParseException &e)  { fprintf(stderr, "ERROR at line %d, column %d\n", e.GetLine(), e.GetLinePos()); }
#ifndef _NO_LEMMATIZER
	catch(CExpc &e)           { fprintf(stderr, "ERROR: %s\n", e.m_strCause.c_str()); }
	catch(STLW::exception &e) { fprintf(stderr, "ERROR: %s\n", e.what()); }
#endif
	catch(...)                { fprintf(stderr, "ERROR: Ouch!\n"); }

	fclose(stdin);
	fclose(stdout);
	fclose(stderr);

return EX_OK;
}
// End.
